package com.qf.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qf.pojo.Address;
import com.qf.pojo.Cart;
import com.qf.pojo.Order;
import com.qf.pojo.User;
import com.qf.service.AddressService;
import com.qf.service.CartService;
import com.qf.service.OrderService;
import com.qf.service.impl.AddressServiceImpl;
import com.qf.service.impl.CartServiceImpl;
import com.qf.service.impl.OrderServiceImpl;

import javax.servlet.annotation.WebServlet;
import java.util.List;
import java.util.Map;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@WebServlet("/orderservlet")
public class OrderServlet extends BaseServlet {

    CartService cartService = new CartServiceImpl();

    AddressService addressService = new AddressServiceImpl();

    OrderService orderService = new OrderServiceImpl();

    /**
     * 支付成功
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String paySuccess(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception {
        //获取支付结果
        String result = request.getParameter("result");

        //把json字符串转换为json对象
        Map map = JSON.parseObject(result, Map.class);

        JSONObject jsonObject = (JSONObject) map.get("result");
        //获取支付结果
        String result_code = (String) jsonObject.get("result_code");
        //如果支付成功了， 更新数据库的订单状态
        if("success".equalsIgnoreCase(result_code)){
            //获取订单编号
            String orderNo = (String) jsonObject.get("out_trade_no");
            //更新订单的状态
            Order order = new Order();
            order.setId(orderNo);
            order.setStatus("已支付");

            orderService.paySuccess(order);
        }

        request.setAttribute("msg", "支付成功!!!");

        return "forward:/message.jsp";
    }
    /**
     * 添加订单
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String addOrder(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception {
        User user = (User) request.getSession().getAttribute("user");
        //地址id
        String aid = request.getParameter("aid");

        Order order = orderService.addOrder(user.getId(), aid);

        request.setAttribute("order", order);
        return "forward:/orderSuccess.jsp";
    }
        //预览 订单信息
    public String getOrderView(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception {

        User user = (User) request.getSession().getAttribute("user");

        //获取 购物车信息
        List<Cart> carts = cartService.findCartByUId(user.getId());

        request.setAttribute("carts", carts);

        //查询该用户的地址列表信息
        List<Address> addList = addressService.findAddListByUid(user.getId());

        request.setAttribute("addList", addList);

        return "forward:/order.jsp";

    }

}